package com.cloud.controller;

import com.cloud.domain.TOrder;
import com.cloud.service.IOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/consumer/hystrix")
@RestController
public class OrderHystrixController {
    @Autowired
    private IOrderService iOrderService;

    /**
     * 熔断降级
     *
     * @param id
     * @return
     */
//    @HystrixCommand(fallbackMethod = "fallback", commandProperties = {
//
//            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"),
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50")
//    })
    @RequestMapping("/getOrder")
    public TOrder getOrder(Long id) {
        if (id == null) {
            return null;
        }
        System.out.println("id:" + id);
        TOrder responseResult = iOrderService.getById(id);
        System.out.println(responseResult);
        return responseResult;
    }

    /**
     * 超时降级
     *
     * @param id
     * @return
     */
//    @HystrixCommand(fallbackMethod = "timoutFallback", commandProperties = {
//            @HystrixProperty(name = "execution.timeout.enabled", value = "true"),
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
//    })
    @RequestMapping("/timeout")
    public TOrder timeout(Long id) {
        if (id == null) {
            return null;
        }
        if (id < 0) {
            int i = 10 / 0;
        }
        TOrder responseResult = iOrderService.timeout(id);
        System.out.println(responseResult);
        return responseResult;
    }

    /**
     * 使用限流降级的方法
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "limitFallback", commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
            @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "10")
    })
    @RequestMapping("/listAll")
    public List<TOrder> listAll() {
        List<TOrder> list = iOrderService.listAll();
        System.out.println(list);
        return list;
    }

    @RequestMapping("/listAll2")
    public List<TOrder> listAll2() {
        List<TOrder> list = iOrderService.listAll();
        System.out.println(list);
        return list;
    }

    public List<TOrder> limitFallback() {
        System.out.println("限流方法！！！");
        return null;
    }

    public TOrder fallback(Long id) {
        System.out.println("降级方法！！！");
        return null;
    }

    public TOrder timoutFallback(Long id) {
        System.out.println("请求超时！！！");
        return null;
    }
}
